// miniprogram/pages/login/login.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    emailtxt:"请输入账号或者手机号码",
    pwdtxt:"请输入密码",
    pwdtype:"password",
    logintext:"验证码登录",
    email:"",
    pwd:"",
    width:'100%',
    btntext:"点击获取验证码",
    hidden:'none',
    cartItems: [],
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  onLoad: function () {
    var cartItems = wx.getStorageSync('cartItems') || []
    for (var i = 0; i < cartItems.length; i++) {
      delete cartItems[i].img_url;
      delete cartItems[i].name;
      delete cartItems[i].old_price;
      delete cartItems[i].price;  
    }

    this.setData({
      cartItems: cartItems
    })
  },

  getEmailValue(e){
    this.setData({
      email:e.detail.value
    })
  },

  getPasswordValue(e) {
    this.setData({
      pwd: e.detail.value
    })
  },

  forgetButton:function(){
    let status = this.data.hidden;
    let that=this;
    if(status == 'none'){
      that.setData({
        logintext:"账号密码登录",
        hidden:'block',
        width:'40%',
        pwdtxt:'请输入验证码',
        pwdtype:'text'
      })
    }else{
      that.setData({
        hidden: 'none',
        logintext: "验证码登录",
        width: '100%',
        pwdtxt:'请输入密码',
        pwdtype: "password"
      })
    }
  } , 

  phoneCode() {
    var that = this;
    var phone = this.data.email;
    wx.request({
      url: app.globalData.url +'/index.php?_m=index/wx/phone_code',
      header: {
       'content-type': 'application/x-www-form-urlencoded'
      },
      data: { phone: phone, type: 'resetpwd' },
      method: "POST",
      success(res) {
    
        if (res.data.code == 0) {
          var coden = 60    // 定义60秒的倒计时
          var codeV = setInterval(function () {
            that.setData({    // _this这里的作用域不同了
              btntext: (--coden) + 's' + '后重新获取'
            })
            if (coden == -1) {  // 清除setInterval倒计时，这里可以做很多操作，按钮变回原样等
              clearInterval(codeV)
              that.setData({
                btntext: '点击获取验证码'
              })
            }
          }, 1000)  //  1000是1秒
        } else {
          wx.showModal({
            title: "提示",
            content: res.data.msg
          })
        }

      },
    })
  },

  loginSubmit:function(e){
    let that = this;
    let email=that.data.email;
    let pwd = that.data.pwd;
    let hidden = that.data.hidden; 
    let cart_json = JSON.stringify(that.data.cartItems);
    let type = "";
    if (hidden == "block"){
        type = "phone_code_login"
    }
    if (!email) {
      wx.showModal({
        title: "提示",
        content: '请输入正确的账号'
      })
      return false;
    }
    wx.showLoading({
      title: '加载中...',
    })
    wx.login({
      success(res) {
        if (res.code) {
          wx.getUserInfo({
            success: function (resInfo) {     
                  wx.request({
                    url: app.globalData.url +'index.php?_m=index/wx/token',
                    header: {
                      'content-type': 'application/x-www-form-urlencoded'
                    },
                    data: {
                      code: res.code,
                      rawData: resInfo.rawData,
                      signature: resInfo.signature,
                      iv:resInfo.iv,
                      encryptedData: resInfo.encryptedData,
                      email: email,
                      pwd: pwd,
                      type:type,
                      cart_json: cart_json
                    },
                    method: "POST",
                    success(res) {
                      wx.hideLoading();
                      if(res.data.code == 0){
                        wx.removeStorageSync("cartItems");
                        app.globalData.token = res.data.token;
                        wx.setStorageSync('token', app.globalData.token);
                        app.globalData.uid = res.data.uid;
                        wx.setStorageSync('uid', app.globalData.uid);
                        app.globalData.email = email;
                        wx.setStorageSync('email', app.globalData.email);
                        app.globalData.pwd = pwd;
                        wx.setStorageSync('pwd', app.globalData.pwd);  
                      wx.reLaunch({
                        url: '/pages/index/index'
                      })
                      }else{
                        wx.showToast({
                          title: res.data.msg,
                          icon: 'none',
                          duration: 3000
                        })
                        setTimeout(function () {
                          wx.hideToast()
                        }, 3000)
                        return false;
                      }
                    }
                  })
            }
          })  
        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.errMsg,
            icon: 'none',
            duration: 3000
          })
        }
      }
    })
 
  },

  bindGetUserInfo:function(e){
    let that = this;
    let email = app.globalData.email;
    let pwd = app.globalData.pwd;
    let cart_json = JSON.stringify(that.data.cartItems);
    
    wx.showLoading({
      title: '加载中...',
    })
    wx.login({
      success(res) {
        if (res.code) {
            wx.getUserInfo({
              success: function (resInfo) {
                  wx.request({
                    url: app.globalData.url +'index.php?_m=index/wx/token',
                    header: {
                      'content-type': 'application/x-www-form-urlencoded'
                    },
                    data: {
                      code: res.code,
                      rawData: resInfo.rawData,
                      signature: resInfo.signature,
                      iv:resInfo.iv,
                      encryptedData: resInfo.encryptedData,
                      type: 'weixin',
                      email: email,
                      pwd: pwd,
                      cart_json: cart_json
                    },
                    method: "POST",
                    success(res) {
                      wx.hideLoading();

                      if (res.data.code == 0) {
                        wx.removeStorageSync("cartItems");
                        app.globalData.token = res.data.token;
                        wx.setStorageSync('token', app.globalData.token);
                        app.globalData.uid = res.data.uid;
                        wx.setStorageSync('uid', app.globalData.uid);
                        wx.reLaunch({
                          url: '/pages/index/index'
                        })
                      } else {
                        wx.showToast({
                          title: res.data.msg,
                          icon: 'none',
                          duration: 3000
                        })
                        setTimeout(function () {
                          wx.hideToast()
                        }, 3000)
                        return false;
                      }
                    }
                  })                
              }
            })
        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.errMsg,
            icon: 'none',
            duration: 3000
          })
      
        }  
      }
    })

  },
  goHome(){
    wx.switchTab({
      url:"../index/index"
    })
  },
  onShareAppMessage: function () {
    app.globalData.uid = app.globalData.uid || wx.getStorageSync('uid') || 0;
    return {
      title: '深圳智汇创想科技有限责任公司员工福利购!',
      path: '/pages/index/index?fid=' + app.globalData.uid
    }
  }

  
})